package com.lipy.java8study.thread;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @Description copy from dubbo taskQueue
 * @Date 2020/11/25 10:33
 * @Created by lipye
 */
public class TaskQueue<R extends Runnable> extends LinkedBlockingQueue<Runnable> {
    private static final long serialVersionUID = -2635853580887179627L;
    private EagerThreadPoolExecutor executor;

    public TaskQueue(int capacity) {
        super(capacity);
    }

    public void setExecutor(EagerThreadPoolExecutor exec) {
        this.executor = exec;
    }

    @Override
    public boolean offer(Runnable runnable) {
        if (this.executor == null) {
            throw new RejectedExecutionException("The task queue does not have executor!");
        } else {
            int currentPoolThreadSize = this.executor.getPoolSize();
            if (this.executor.getSubmittedTaskCount() < currentPoolThreadSize) {
                return super.offer(runnable);
            } else {
                return currentPoolThreadSize < this.executor.getMaximumPoolSize() ? false : super.offer(runnable);
            }
        }
    }

    public boolean retryOffer(Runnable o, long timeout, TimeUnit unit) throws InterruptedException {
        if (this.executor.isShutdown()) {
            throw new RejectedExecutionException("Executor is shutdown!");
        } else {
            return super.offer(o, timeout, unit);
        }
    }
}